<?php include_once('../config/db.php'); 
	   include_once('../functions/item.php');
	   include_once('../functions/category.php');
	if(isset($_REQUEST['submit_noq'])){
		$noq = $_REQUEST['noq'];
		if($noq >= 1) $var = 1;
		
	}
	
	if(isset($_REQUEST['save'])){
		$r = addItem($_REQUEST['i']);
		
		if($r['inserted'] > 0)
			$_SESSION['insert_success'] = $r['inserted'].' item/s added to the database';
		else
			$_SESSION['insert_failed'] = $r['inserted'].' item/s added to the database';
		if(isset($r['duplicate']))
			$_SESSION['duplicate'] = $r['duplicate'];
			
		/*
		$item_name=$_REQUEST['item_name'];
		$item_serial=$_REQUEST['item_serial'];
		$item_desc=$_REQUEST['item_desc'];
		$item_status=$_REQUEST['item_status'];
		if($item_name=='' && $item_name=='' && $item_serial==''){
			$empty=TRUE;
		}
		elseif(check_item($item_serial)==$item_serial){
			$check_serial=TRUE;
		}
		else{
			$insert=add_item($item_name, $item_serial, $item_desc, $item_status);
			$success=TRUE;
		
		}
		*/
	}
	
	$cat = getCategories();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../style.css" type="text/css"/>
<title>Untitled Document</title>
</head>
<body>
<div id="header">
<div id="logo">
  <p>&nbsp;</p>
  <div id="apDiv1"><img src="../image/logo.png" alt="" width="500" height="100" /></div>
  <p>&nbsp;</p>
</div>
</div>
<div id="con-body">
<?php include 'navigation.php';?>  
  
  <?php if(isset($_SESSION['insert_success'])):?>
  		<div class="success">Succes: <?php echo $_SESSION['insert_success']?></div>
	<?php 
		unset($_SESSION['insert_success']);
		elseif(isset($_SESSION['insert_failed'])):
	?>
		<div class="success">Failed: <?php echo $_SESSION['insert_failed']?></div>
	<?php
		unset($_SESSION['insert_failed']);
	 	endif;
	?>
	
	 <?php if(isset($_SESSION['duplicate'])):?>
  		<div class="error">Error: <?php echo $_SESSION['duplicate']?></div>
	<?php 
		unset($_SESSION['duplicate']);
	 	endif;
	?>

	<?php if(!isset($var)):?>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    	<label>Select no. of items you want to add.</label>
    	<select name="noq">
        	
        	<?php for($v = 1; $v <= 50; $v++):?>
        	<option value="<?php echo $v;?>"><?php echo $v;?></option>
            <?php endfor;?>
        </select>
        <input type="submit" name="submit_noq" value="Submit" class="submit" />
    </form>
    <?php endif;?>
    
    <?php if(isset($var)):?>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    	<input type="hidden" name="noq" value="<?php echo $noq;?>"/>
     <?php for($i = 1; $i<=$noq; $i++):?>
		<table width="437" border="0">
    <tr>
      <td>Item Name: </td>
      <td><input name="i[<?php echo $i; ?>][name]" type="text" id="item_name" size="40" /></td>
    </tr>
	<tr>
      <td>Category: </td>
      <td>
	  	<select name="i[<?php echo $i?>][category_id]">
			<?php if($cat != 0 ):?>
				<?php foreach($cat as $c):?>
					<option value="<?php echo $c['category_id']?>"><?php echo $c['category_name'];?></option>
				<?php endforeach;?>
			<?php else:?>
				<option>No categories yet.</option>
			<?php endif; ?>
		</select>
	 </td>
    </tr>
	<tr>
      <td>Serial #:</td>
      <td><input name="i[<?php echo $i; ?>][serial]" type="text" id="item_serial" size="40" /></td>
    </tr>
    <tr>
      <td>Description: </td>
      <td><textarea name="i[<?php echo $i; ?>][desc]" cols="40" rows="5" id="item_desc"></textarea>
        <input type="hidden" name="item_status" id="item_status" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td></td>
    </tr>
</table> <?php endfor;?>

           <!--<input type="checkbox" name="open_to<?php //echo $i;?>[]" value="kinder" /> Kinder
            <input type="checkbox" name="open_to<?php //echo $i;?>[]" value="grade1" /> Grade 1
            
        	<label for="fasd">Question</label>
        	<textarea name="q<?php //echo $i; ?>" rows="10%" cols="10%" ></textarea>-->
    <input name="save" type="submit" class="blue" id="save" value="Save Materials" /></form><?php endif;?>
</div>

<p></p>
</body>
</html>